package yuhao.jdbc2;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import yuhao.pojo.Brand;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;

public class Jdbcbrand {
    public static void main(String[] args) throws Exception {
        //selectall();
        //update();
        delete();
        //insert();


    }

    private static void update() throws Exception {
        String company_name = "宇宙";

        InputStream is = Jdbcbrand.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties prop = new Properties();
        prop.load(is);

        DataSource ds = DruidDataSourceFactory.createDataSource(prop);

        Connection conn = ds.getConnection();
        String sql = "update tb_brand set company_name=? where id=2;";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setString(1, company_name);
        int i = pre.executeUpdate();
        System.out.println(i);

    }

    private static void insert() throws Exception {
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        Integer ordered = 1;
        String description = "绕地球一圈";
        Integer status = 1;
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
        Properties prop = new Properties();
        prop.load(is);

        DataSource ds = DruidDataSourceFactory.createDataSource(prop);

        Connection conn = ds.getConnection();
        String sql = "insert into  tb_brand(brand_name, company_name, ordered, description, status) VALUES(?,?,?,?,?)";
        PreparedStatement pre = conn.prepareStatement(sql);
        pre.setString(1, "香飘飘");
        pre.setString(2, "香飘飘");
        pre.setInt(3, 1);
        pre.setString(4, "绕地球一圈");
        pre.setInt(5, 1);
        int i = pre.executeUpdate();
        System.out.println(i);
        InputStream is2 = ClassLoader.getSystemResourceAsStream("druid.properties");
        Properties properties = new Properties();
        properties.load(is2);
    }

    private static void delete() throws Exception {
        Integer id = 3;
        //加载配置文件
        InputStream is = Jdbcbrand.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties prop = new Properties();
        prop.load(is);
        //------------------------------------------------------------------------------------------------------------------------------
//获取数据库连接对象
        DataSource ds = DruidDataSourceFactory.createDataSource(prop);
        Connection conn = ds.getConnection();
//------------------------------------------------------------------------------------------------------------------------------
        String sql = "delete from tb_brand where id=?";

        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1, 1);
        int i = preparedStatement.executeUpdate();
        System.out.println(i);
    }

    public static void selectall() throws Exception {
        InputStream is = Jdbcbrand.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties prop = new Properties();
        prop.load(is);

        DataSource ds = DruidDataSourceFactory.createDataSource(prop);

        Connection conn = ds.getConnection();

        String sql = "select * from  tb_brand";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();

        ArrayList<Brand> list = new ArrayList<>();
        while (rs.next()) {
            Integer id = rs.getInt("id");
            String brandName = rs.getString("brand_name");
            String companyname = rs.getString("company_name");
            Integer ordered = rs.getInt("ordered");
            String description = rs.getString("description");
            int status = rs.getInt("status");

            Brand a = new Brand(id, brandName, companyname, ordered, description, status);

            list.add(a);

        }
        rs.close();
        conn.close();
        ps.close();

        for (Brand brand : list) {
            System.out.println(brand);
        }
    }
}
